package com.zjb.demo.common.config;

import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.framework.security.JwtAuthenticationTokenProcessor;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.auth.AuthAdapter;

/**
 * @author 庄佳彬
 * @since 2023/12/29 12:15
 */
@Configuration
public class AuthConfig {
    @Bean
    public AuthAdapter init(@Inject JwtAuthenticationTokenProcessor processor) {
        //设定默认的验证失败处理；也可以用过滤器捕促异常
        //"/version", "/login", "/register", "/captchaImage", "/refreshToken"
        return new AuthAdapter()
//                .addRule(b -> b.include("/getInfo"))//
                .addRule(b -> b.exclude("/login**")
                        .exclude("/captchaImage")
                        .exclude("/register**")
                        .exclude("/refreshtoken**")
                        .exclude("/version")
                        .exclude("/logout")
                        .verifyLogined())
                .processor(processor) //设定认证处理器
                .failure(((ctx, rst) ->
                        ctx.render(AjaxResult.fail(rst.getCode(), "认证失败"))
                ));

    }
}
